﻿using System.Collections.Generic;
using JPR.TunelProxy.Interfaces;

namespace JPR.TunelProxy.Proxy.Implementations
{
	public class CloseConnectionHttpHeadersFormatter : IHttpHeadersFormatter
	{
		private const string ConnectionKey = "Connection";
		private const string CloseConnectionValue = "close";

		#region Implementation of IHttpHeadersFormatter

		public Dictionary<string, string> ProcessHeadersToServer(IDictionary<string, string> headers)
		{
			Dictionary<string, string> ret = new Dictionary<string, string>();
			foreach (KeyValuePair<string, string> header in headers)
			{
				if (header.Key == ConnectionKey)
				{
					ret.Add(header.Key, CloseConnectionValue);
				}
				else
				{
					ret.Add(header.Key, header.Value);
				}
			}
			return ret;
		}

		public Dictionary<string, string> ProcessHeadersFromServer(IDictionary<string, string> headers)
		{
			Dictionary<string, string> ret = new Dictionary<string, string>();
			foreach (KeyValuePair<string, string> header in headers)
			{
				ret.Add(header.Key, header.Value);
			}
			return ret;
		}

		#endregion
	}
}